//
//  MerchantsOpinionController.m
//  MagazineShangejie
//
//  Created by mac on 15/3/18.
//  Copyright (c) 2015年 CnbizMedia. All rights reserved.
//

#import "MerchantsOpinionController.h"
#import "Util.h"
@interface MerchantsOpinionController (){
    UILabel*textViewPlaceholder;
}

@end

@implementation MerchantsOpinionController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initPlaceholder];
    [self initRightBarButton];
    [self initUserData];
    // Do any additional setup after loading the view.
}
-(void)initUserData{
    NSDictionary*dic=[UserDefaults objectForKey:@"user"];
    if (dic[@"mobile"]) {
        self.phone.text=dic[@"mobile"];
    }
    NSArray *arr = [dic[@"email"] componentsSeparatedByString:@"@"];
    if (![arr[1] isEqualToString:@"ksj.com"]) {
        self.email.text=dic[@"email"];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)initRightBarButton{
    UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
    [button setFrame:CGRectMake(0, 0, 40, 40)];
    [button setTitle:@"提交" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(commit) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem*rightItem = [[UIBarButtonItem alloc]initWithCustomView:button];
    
    self.navigationItem.rightBarButtonItem= rightItem;
    
}
-(void)initPlaceholder{
    self.title=@"留言";
    textViewPlaceholder=[[UILabel alloc]initWithFrame:CGRectMake(5, 10, 200, 20)];
    textViewPlaceholder.textColor=[UIColor colorWithWhite:0.729 alpha:0.820];
    textViewPlaceholder.text=@"请写下你的问题和建议吧";
    [self.Content addSubview:textViewPlaceholder];
    
    
}

-(void)commit{
    [[AppAPIClient sharedClient] POST:@"Comment/Add" parameters:@{@"email":self.email.text,@"content":self.Content.text,@"mobile":self.phone.text} success:^(AFHTTPRequestOperation *operation, id responseObject) {
        //                int code = [responseObject[@"code"] intValue];
        [SVProgressHUD showSuccessWithStatus:@"提交成功"];
        //            self.email.text=@"";
        //            self.phone.text=@"";
        //            self.Content.text=@"";
        //                NSLog(@"%@",responseObject);
        //                [SVProgressHUD showSuccessWithStatus:LOADING_TEXT_SUCCESS];
        [self.navigationController popViewControllerAnimated:YES];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [SVProgressHUD showErrorWithStatus:LOADING_TEXT_ERROR];
        NSLog(@"Error:%@ => %@",error,operation.responseString);
    }];

}

#pragma mark - textView Delegate
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text

{
    
    if ([text isEqualToString:@"\n"]) {//检测到“完成”
        
        [textView resignFirstResponder];//释放键盘
        
        return NO;
        
    }
    
    if (textView.text.length==0){//textview长度为0
        
        if ([text isEqualToString:@""]) {//判断是否为删除键
            
            textViewPlaceholder.hidden=NO;//隐藏文字
            
        }else{
            
            textViewPlaceholder.hidden=YES;
            
        }
        
    }else{//textview长度不为0
        
        if (textView.text.length==1){//textview长度为1时候
            
            if ([text isEqualToString:@""]) {//判断是否为删除键
                
                textViewPlaceholder.hidden=NO;
                
            }else{//不是删除
                
                textViewPlaceholder.hidden=YES;
                
            }
            
        }else{//长度不为1时候
            
            textViewPlaceholder.hidden=YES;
            
        }
        
    }
    
    return YES;
    
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [self resignFirst];
}
-(void)resignFirst{
    [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
